<?php

namespace app\admin\controller;

use app\service\RoleService;
use app\service\UserService;
use app\admin\validate\userValidate;
use think\exception\ValidateException;
USE app\model\Role;
class User extends Admin
{
    public function __construct(UserService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    /**
     * 用户列表
     * @return \think\response\Json|string
     */
    public function index()
    {
        if (!$this->request->isPost()) {
            $this->assign("title", "用户列表");
            return $this->fetch("index");
        }
        $params = $this->request->post();
        $where = [];
        if (!empty($params["status"])) {
            $where[] = ['status', '=', $params["status"]];
        }
        if (!empty($params["username"])) {
            $where[] = ['username', 'like', "%{$params['username']}%"];
        }
        if (!empty($params["nickname"])) {
            $where[] = ['nickname', 'like', "%{$params['nickname']}%"];
        }
        if (!empty($params["login_at"])) {
            $between = explode(" - ", $params["login_at"]);
            if (count($between) == 2) {
                $where[] = ['login_at', '>=', $between[0]];
                $where[] = ['login_at', '<=', $between[1]];
            }
        }
        $ret = $this->service->getList($where);
        $this->result($ret);
    }

    /**
     * 保存用户
     * @return string|\think\response\Json
     * @throws ParameterException
     */
    public function edit()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get("id");
            if (!$id) {
                throw new ValidateException("缺少id");
            }
            $vo = $this->service->get($id);
            $roles = [];
            foreach ($vo->roles as $v) {
                array_push($roles, $v->id);
            }
            $vo->roles = $roles;
            $this->assign("vo", $vo);
            $roleService = $this->app->make(RoleService::class);
            $roles = $roleService->getAll(["status" =>Role::ROLE_STATUS_ACTIVE], "id,name,desc,status");
            $this->assign("roles", $roles);
            $this->assign("is_administrator", is_administrator($id));
            return $this->fetch("form");
        }
        $params = $this->request->Post();
        try {
            validate(userValidate::class)->scene("edit")->check($params);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        $res = $this->service->saveAll($params);
        if (!$res) {
            $this->error("保存失败");
        }
        $this->success("保存成功");
    }

    /**
     * 添加用户
     * @return string|\think\response\Json
     */
    public function add()
    {
        if (!$this->request->isPost()) {
            $this->assign("vo", null);
            $roleService = $this->app->make(RoleService::class);
            $roles = $roleService->getAll(["status" =>Role::ROLE_STATUS_ACTIVE], "id,name,desc,status");
            $this->assign("roles", $roles);
            $this->assign("is_administrator", false);
            return $this->fetch("form");
        }
        $params = $this->request->Post();
        try {
            validate(userValidate::class)->scene("add")->check($params);
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }
        $res = $this->service->saveAll($params);
        if (!$res) {
            $this->error("保存失败");
        }
        $this->success("保存成功");
    }

    /**
     * 设置状态
     * @return \think\response\Json
     */

    public function status()
    {
        $params = $this->request->Post();
        try {
            validate(userValidate::class)->scene("status")->check($params);
        } catch (ValidateException $e) {
            $this->error($e->getError());
        }
        $ret = $this->service->setStatus($params["id"], $params["status"]);
        if ($ret) {
            $this->success("设置成功");
        } else {
            $this->error("设置失败");
        }
    }

    /**
     * 修改密码
     * @return string|\think\response\Json
     * @throws ParameterException
     */
    public function passwd()
    {
        if (!$this->request->isPost()) {
            $id = $this->request->get("id");
            if (!$id) {
                throw new ValidateException("缺少id");
            }
            $vo = $this->service->get($id);
            $this->assign("vo", $vo);
            return $this->fetch("passwd");
        }
        $params = $this->request->Post();
        try {
            validate(userValidate::class)->scene("passwd")->check($params);
        } catch (ValidateException $e) {
            return $this->error($e->getError());
        }
        $ret = $this->service->savePasswd($params);
        if ($ret) {
            $this->success("设置成功");
        } else {
            $this->error("设置失败");
        }
    }

}